package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.algo.DataSet;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 银行借款查询报表
 * xiao
 * nckd_bankloansumquery
 */

public class BankLoanSumDetailQueryPlugin extends AbstractReportListDataPlugin {

    //银行贷款类型
    private final String[] loanType = {"loan","sl","bond"};

    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable {

        QFilter[] billFilters = null;
        //单据状态=审核
        QFilter qFilter1 = new QFilter("billstatus", QCP.equals,"C");
        //贷款类型=银行
        QFilter qFilter2 = new QFilter("loantype", QCP.in,loanType);

        //提款处理查询字段
        String field = "id,billno as nckd_number,repayplan_entry,org.name as nckd_name,bizdate as nckd_date,billstatus,loantype,repayplan_entry.exdrawamount as nckd_amount,repayamount as nckd_repayamount, drawamount as nckd_drawamount,repayplan_entry.exrepaymentdate as nckd_date1";
        billFilters = new QFilter[]{qFilter1,qFilter2};
        DataSet dataSet1 = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cfm_loanbill", field, billFilters, null);
        dataSet1 = dataSet1.select("id,nckd_number,nckd_name,nckd_date,nckd_amount,nckd_repayamount,nckd_drawamount,nckd_date1,(id + to_char(nckd_date1,'yyyyMMdd')) as month");

        Map<String, Object> customParam = reportQueryParam.getCustomParam();
        List<String> list = (ArrayList<String>) customParam.get("id");
        //List<Long> longList = list.stream().map(e -> Long.valueOf(e)).collect(Collectors.toList());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            String id = list.get(i);
            if (list.size() - 1 == i) {
                sb.append("'"+id+"'");
            } else {
                sb.append("'"+id+"'").append(",");
            }
        }
        dataSet1 = dataSet1.filter("month in (" + sb + ")");
        return dataSet1;
    }
}
